package com.steerinfo.dil.config;


import com.alibaba.fastjson.JSONObject;
import com.steerinfo.dil.aspect.LogAspect;
import lombok.extern.slf4j.Slf4j;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLDecoder;

@Component
public class RequestFilter implements Filter {
    static final Logger log = Logger.getLogger(RequestFilter.class);

    @Override
    public void destroy() {}

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
       try{
           HttpServletRequest httpRequest = (HttpServletRequest) request;
           //获取参数，并校验
           Cookie[] cookies = httpRequest.getCookies();
           String userId = httpRequest.getHeader("userid");
           String userName = httpRequest.getHeader("username");
           String orgCode = httpRequest.getHeader("orgcode");
           String roleCodes = httpRequest.getHeader("rolecodes");
           //获取cookie用户数据
           if (cookies != null && cookies.length > 0) {
               for (Cookie cookie : cookies) {
                   if (cookie.getName().equals("userId") && userId == null) {
                       userId = cookie.getValue();
                   } else if (cookie.getName().equals("userName") && userName == null) {
                       userName = URLDecoder.decode(cookie.getValue(), "utf-8");
                   } else if (cookie.getName().equals("orgCode") && orgCode == null) {
                       orgCode = URLDecoder.decode(cookie.getValue(), "utf-8");
                   } else if (cookie.getName().equals("roleCodes") && roleCodes == null) {
                       roleCodes = URLDecoder.decode(cookie.getValue(), "utf-8");
                   }else if (userId != null && userName != null && orgCode != null && roleCodes != null) {
                       //都查到了，退出
                       break;
                   }
               }
           }
           try {
               userName = URLDecoder.decode(userName, "utf-8");
               orgCode = URLDecoder.decode(orgCode, "utf-8");
               roleCodes = URLDecoder.decode(roleCodes, "utf-8");
           }catch (Exception e){}
           httpRequest.setAttribute("userId", userId);
           httpRequest.setAttribute("userName", userName);
           httpRequest.setAttribute("orgCode", orgCode);
           httpRequest.setAttribute("roleCodes", roleCodes);
           //获取RequestBody数据
           BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
           StringBuilder strBuilder = new StringBuilder();
           String inputStr;
           while ((inputStr = streamReader.readLine()) != null) {
               strBuilder.append(inputStr);
           }
           if (strBuilder != null && !"".equals(strBuilder.toString())) {
               //有请求体则写入userId和userName，保存
               JSONObject params = JSONObject.parseObject(strBuilder.toString());
               params.put("userId", userId);
               params.put("userName", userName);
               params.put("orgCode", orgCode);
               params.put("roleCodes",roleCodes);
               //将新的RequestBody写入新的请求对象保存
               ServletRequest requestWrapper = new RequestWrapper(httpRequest, params.toJSONString());
               chain.doFilter(requestWrapper, response);
           } else {
               chain.doFilter(request, response);
           }
       } catch (Exception e) {
           e.printStackTrace();
           chain.doFilter(request, response);
       }
    }
    @Override
    public void init(FilterConfig arg0) throws ServletException {}
}

